package com.systop.config;

import com.rabbitmq.client.Channel;
import com.systop.entity.Order;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.Exchange;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.rabbit.annotation.EnableRabbit;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: TODO
 * @Author: jsj
 * @Date: 2023/3/21
 */
@Configuration
@EnableRabbit
public class MQConfig {

    @Bean
    public Queue orderDelayQueue(){
        //设置参数
        Map<String,Object> arguments = new HashMap<>();
        //设置死信路由
        arguments.put("x-dead-letter-exchange","order-event-exchange");
        //设置路由键
        arguments.put("x-dead-letter-routing-key", "order.release.order");
        //设置消息过期时间
        arguments.put("x-message-ttl",60000);

        //创建队列
        Queue queue = new Queue("order.delay.queue", true, false, false,arguments);

        //返回队列
        return queue;
    }

    @Bean
    public Queue orderReleaseOrderQueue(){

        //创建队列
        Queue queue = new Queue("order.release.order.queue", true, false, false);

        //返回队列
        return queue;
    }

    @Bean
    public Exchange orderEventExchange(){
        return new TopicExchange("order-event-exchange",true,false);
    }

    @Bean
    public Binding orderCreateOrderBinding(){
        return new Binding("order.delay.queue", Binding.DestinationType.QUEUE,"order-event-exchange",
                "order.create.order",null);
    }

    @Bean
    public Binding orderReleaseOrderBinding(){
        return new Binding("order.release.order.queue", Binding.DestinationType.QUEUE,"order-event-exchange",
                "order.release.order",null);
    }


    @Bean
    public Exchange stockEventExchange(){
        return new TopicExchange("stock-event-exchange",true,false);
    }

    @Bean
    public Queue stockReleaseStockQueue(){
        return new Queue("stock.release.stock.queue", true, false, false);
    }

    @Bean
    public Queue stockDelayQueue(){
        //设置参数
        Map<String,Object> arguments = new HashMap<>();
        //设置死信路由
        arguments.put("x-dead-letter-exchange","stock-event-exchange");
        //设置路由键
        arguments.put("x-dead-letter-routing-key", "stock.release");
        //设置消息过期时间
        arguments.put("x-message-ttl",120000);

        return new Queue("stock.delay.queue",true,false,false,arguments);
    }

    @Bean
    public Binding stockReleaseBinding(){
        return new Binding("stock.release.stock.queue",Binding.DestinationType.QUEUE,
                "stock-event-exchange","stock.release.#",null);
    }

    @Bean
    public Binding stockLockedBinding(){
        return new Binding("stock.delay.queue",Binding.DestinationType.QUEUE,
                "stock-event-exchange","stock.locked",null);
    }


    @Bean
    public Binding orderReleaseOtherBinding(){
        return new Binding("stock.release.stock.queue",Binding.DestinationType.QUEUE,
                "order-event-exchange","order.release.other.#",null);
    }

}
